{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('../../..')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/mandubian/.cache/pypoetry/virtualenvs/codenets-Xapi4pwr-py3.7/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint8 = np.dtype([(\"qint8\", np.int8, 1)])\n",
      "/home/mandubian/.cache/pypoetry/virtualenvs/codenets-Xapi4pwr-py3.7/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint8 = np.dtype([(\"quint8\", np.uint8, 1)])\n",
      "/home/mandubian/.cache/pypoetry/virtualenvs/codenets-Xapi4pwr-py3.7/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint16 = np.dtype([(\"qint16\", np.int16, 1)])\n",
      "/home/mandubian/.cache/pypoetry/virtualenvs/codenets-Xapi4pwr-py3.7/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint16 = np.dtype([(\"quint16\", np.uint16, 1)])\n",
      "/home/mandubian/.cache/pypoetry/virtualenvs/codenets-Xapi4pwr-py3.7/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint32 = np.dtype([(\"qint32\", np.int32, 1)])\n",
      "/home/mandubian/.cache/pypoetry/virtualenvs/codenets-Xapi4pwr-py3.7/lib/python3.7/site-packages/tensorflow/python/framework/dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  np_resource = np.dtype([(\"resource\", np.ubyte, 1)])\n",
      "To use data.metrics please install scikit-learn. See https://scikit-learn.org/stable/index.html\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import sys\n",
    "from pathlib import Path\n",
    "from typing import Tuple\n",
    "import torch\n",
    "import numpy as np\n",
    "from docopt import docopt\n",
    "from dpu_utils.utils import run_and_debug\n",
    "from loguru import logger\n",
    "import pandas as pd\n",
    "from annoy import AnnoyIndex\n",
    "from tqdm import tqdm\n",
    "import shutil\n",
    "from wandb.apis import InternalApi\n",
    "import wandb\n",
    "from codenets.codesearchnet.training_ctx import CodeSearchTrainingContext\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "os.environ[\"WANDB_MODE\"] = \"dryrun\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "restore_dir = \"../../../checkpoints/code_search_bert_query_code_siamese_2020_02_12_00_00/code_search_bert_query_code_siamese_2020_02_12_00_00_best_mrr\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2020-02-13 16:41:39.389 | DEBUG    | __main__:<module>:1 - Building Training Context\n",
      "2020-02-13 16:41:39.390 | INFO     | __main__:<module>:3 - Restoring Training Context from directory../../../checkpoints/code_search_bert_query_code_siamese_2020_02_12_00_00/code_search_bert_query_code_siamese_2020_02_12_00_00_best_mrr\n",
      "2020-02-13 16:41:39.390 | DEBUG    | codenets.recordable:load:226 - RecordableMapping - Loading config\n",
      "2020-02-13 16:41:39.391 | INFO     | codenets.recordable:load:118 - Loading Config File from ../../../checkpoints/code_search_bert_query_code_siamese_2020_02_12_00_00/code_search_bert_query_code_siamese_2020_02_12_00_00_best_mrr/codenets.codesearchnet.query_code_siamese.training_ctx.QueryCodeSiameseCtx/config/codenets.recordable.HoconConfigRecordable/config.conf\n",
      "2020-02-13 16:41:39.486 | DEBUG    | codenets.recordable:load:226 - RecordableMapping - Loading model_optimizer\n",
      "2020-02-13 16:41:39.487 | DEBUG    | codenets.codesearchnet.training_ctx:load:100 - Loading codenets.codesearchnet.query_code_siamese.training_ctx.QueryCodeSiameseModelAndAdamW & AdamW optimizer from ../../../checkpoints/code_search_bert_query_code_siamese_2020_02_12_00_00/code_search_bert_query_code_siamese_2020_02_12_00_00_best_mrr/codenets.codesearchnet.query_code_siamese.training_ctx.QueryCodeSiameseCtx/model_optimizer/codenets.codesearchnet.query_code_siamese.training_ctx.QueryCodeSiameseModelAndAdamW\n",
      "2020-02-13 16:41:39.487 | INFO     | codenets.recordable:runtime_load_recordable_mapping:201 - Loading ../../../checkpoints/code_search_bert_query_code_siamese_2020_02_12_00_00/code_search_bert_query_code_siamese_2020_02_12_00_00_best_mrr/codenets.codesearchnet.query_code_siamese.training_ctx.QueryCodeSiameseCtx/model_optimizer/codenets.codesearchnet.query_code_siamese.training_ctx.QueryCodeSiameseModelAndAdamW/codenets.codesearchnet.query_code_siamese.model.QueryCodeSiamese/encoder\n",
      "2020-02-13 16:41:39.488 | DEBUG    | codenets.codesearchnet.huggingface.models:load:37 - Loading BertModel from ../../../checkpoints/code_search_bert_query_code_siamese_2020_02_12_00_00/code_search_bert_query_code_siamese_2020_02_12_00_00_best_mrr/codenets.codesearchnet.query_code_siamese.training_ctx.QueryCodeSiameseCtx/model_optimizer/codenets.codesearchnet.query_code_siamese.training_ctx.QueryCodeSiameseModelAndAdamW/codenets.codesearchnet.query_code_siamese.model.QueryCodeSiamese/encoder/codenets.codesearchnet.huggingface.models.PreTrainedModelRecordable\n",
      "2020-02-13 16:41:39.958 | INFO     | codenets.recordable:runtime_load_recordable_mapping:201 - Loading ../../../checkpoints/code_search_bert_query_code_siamese_2020_02_12_00_00/code_search_bert_query_code_siamese_2020_02_12_00_00_best_mrr/codenets.codesearchnet.query_code_siamese.training_ctx.QueryCodeSiameseCtx/model_optimizer/codenets.codesearchnet.query_code_siamese.training_ctx.QueryCodeSiameseModelAndAdamW/codenets.codesearchnet.query_code_siamese.model.QueryCodeSiamese/pooler\n",
      "2020-02-13 16:41:39.958 | DEBUG    | codenets.recordable:load:60 - Loading codenets.codesearchnet.poolers.MeanWeightedPooler from ../../../checkpoints/code_search_bert_query_code_siamese_2020_02_12_00_00/code_search_bert_query_code_siamese_2020_02_12_00_00_best_mrr/codenets.codesearchnet.query_code_siamese.training_ctx.QueryCodeSiameseCtx/model_optimizer/codenets.codesearchnet.query_code_siamese.training_ctx.QueryCodeSiameseModelAndAdamW/codenets.codesearchnet.query_code_siamese.model.QueryCodeSiamese/pooler/codenets.codesearchnet.poolers.MeanWeightedPooler\n",
      "2020-02-13 16:41:40.470 | DEBUG    | codenets.recordable:load:226 - RecordableMapping - Loading tokenizer\n",
      "2020-02-13 16:41:40.471 | DEBUG    | codenets.codesearchnet.huggingface.tokenizer_recs:load:161 - HuggingfaceBPETokenizerRecordable - Loading from ../../../checkpoints/code_search_bert_query_code_siamese_2020_02_12_00_00/code_search_bert_query_code_siamese_2020_02_12_00_00_best_mrr/codenets.codesearchnet.query_code_siamese.training_ctx.QueryCodeSiameseCtx/tokenizer/codenets.codesearchnet.huggingface.tokenizer_recs.HuggingfaceBPETokenizerRecordable\n",
      "2020-02-13 16:41:40.532 | DEBUG    | codenets.recordable:load:226 - RecordableMapping - Loading training_params\n",
      "2020-02-13 16:41:40.533 | INFO     | codenets.recordable:load:175 - Loading State dict from ../../../checkpoints/code_search_bert_query_code_siamese_2020_02_12_00_00/code_search_bert_query_code_siamese_2020_02_12_00_00_best_mrr/codenets.codesearchnet.query_code_siamese.training_ctx.QueryCodeSiameseCtx/training_params/codenets.recordable.DictRecordable\n",
      "2020-02-13 16:41:40.534 | INFO     | codenets.codesearchnet.training_ctx:__init__:135 - Activating Tensorboard\n",
      "2020-02-13 16:41:40.534 | DEBUG    | codenets.tensorboard_utils:__init__:53 - Writing TensorBoard events locally to runs/code_search_bert_query_code_siamese_2020_02_12_00_00\n",
      "2020-02-13 16:41:40.535 | INFO     | codenets.codesearchnet.training_ctx:__init__:145 - Activating WanDB\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "\n",
       "            Using <a href=\"https://wandb.com\" target=\"_blank\">Weights & Biases</a> in dryrun mode. Not logging results to the cloud.<br/>\n",
       "            Call wandb.login() to authenticate this machine.<br/>\n",
       "        "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "wandb: Wandb version 0.8.27 is available!  To upgrade, please run:\n",
      "wandb:  $ pip install wandb --upgrade\n",
      "2020-02-13 16:41:40.676 | INFO     | codenets.codesearchnet.training_ctx:__init__:180 - Re-launching training from epoch: 0 with loss:inf mrr:0.0\n"
     ]
    }
   ],
   "source": [
    "logger.debug(\"Building Training Context\")\n",
    "training_ctx: CodeSearchTrainingContext\n",
    "logger.info(f\"Restoring Training Context from directory{restore_dir}\")\n",
    "training_ctx = CodeSearchTrainingContext.build_context_from_dir(restore_dir)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2020-02-13 16:43:07.091 | INFO     | __main__:<module>:6 - queries: ['<qy> convert int to string', '<qy> priority queue', '<qy> string to date', '<qy> sort string list', '<qy> save list to file', '<qy> postgresql connection', '<qy> confusion matrix', '<qy> set working directory', '<qy> group by count', '<qy> binomial distribution', '<qy> aes encryption', '<qy> linear regression', '<qy> socket recv timeout', '<qy> write csv', '<qy> convert decimal to hex', '<qy> export to excel', '<qy> scatter plot', '<qy> convert json to csv', '<qy> pretty print json', '<qy> replace in file', '<qy> k means clustering', '<qy> connect to sql', '<qy> html encode string', '<qy> finding time elapsed using a timer', '<qy> parse binary file to custom class', '<qy> get current ip address', '<qy> convert int to bool', '<qy> read text file line by line', '<qy> get executable path', '<qy> httpclient post json', '<qy> get inner html', '<qy> convert string to number', '<qy> format date', '<qy> readonly array', '<qy> filter array', '<qy> map to json', '<qy> parse json file', '<qy> get current observable value', '<qy> get name of enumerated value', '<qy> encode url', '<qy> create cookie', '<qy> how to empty array', '<qy> how to get current date', '<qy> how to make the checkbox checked', '<qy> initializing array', '<qy> how to reverse a string', '<qy> read properties file', '<qy> copy to clipboard', '<qy> convert html to pdf', '<qy> json to xml conversion', '<qy> how to randomly pick a number', '<qy> normal distribution', '<qy> nelder mead optimize', '<qy> hash set for counting distinct elements', '<qy> how to get database table name', '<qy> deserialize json', '<qy> find int in string', '<qy> get current process id', '<qy> regex case insensitive', '<qy> custom http error response', '<qy> how to determine a string is a valid word', '<qy> html entities replace', '<qy> set file attrib hidden', '<qy> sorting multiple arrays based on another arrays sorted order', '<qy> string similarity levenshtein', '<qy> how to get html of website', '<qy> buffered file reader read text', '<qy> encrypt aes ctr mode', '<qy> matrix multiply', '<qy> print model summary', '<qy> unique elements', '<qy> extract data from html content', '<qy> heatmap from 3d coordinates', '<qy> get all parents of xml node', '<qy> how to extract zip file recursively', '<qy> underline text in label widget', '<qy> unzipping large files', '<qy> copying a file to a path', '<qy> get the description of a http status code', '<qy> randomly extract x items from a list', '<qy> convert a date string into yyyymmdd', '<qy> convert a utc time to epoch', '<qy> all permutations of a list', '<qy> extract latitude and longitude from given input', '<qy> how to check if a checkbox is checked', '<qy> converting uint8 array to image', '<qy> memoize to disk  - persistent memoization', '<qy> parse command line argument', '<qy> how to read the contents of a .gz compressed file?', '<qy> sending binary data over a serial connection', '<qy> extracting data from a text file', '<qy> positions of substrings in string', '<qy> reading element from html - <td>', '<qy> deducting the median from each column', '<qy> concatenate several file remove header lines', '<qy> parse query string in url', '<qy> fuzzy match ranking', '<qy> output to html file', '<qy> how to read .csv file in an efficient way?']\n"
     ]
    }
   ],
   "source": [
    "queries = pd.read_csv(training_ctx.queries_file)\n",
    "queries = list(map(lambda q: f\"<qy> {q}\", queries[\"query\"].values))\n",
    "queries_tokens, queries_masks = training_ctx.tokenize_query_sentences(\n",
    "    queries, max_length=training_ctx.conf[\"dataset.common_params.query_max_num_tokens\"]\n",
    ")\n",
    "logger.info(f\"queries: {queries}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
